﻿using System;

namespace Loong.Events.Bus.Handlers
{
    internal class HandlerUnregistrar : IDisposable
    {
        private readonly IEventBus _eventBus;
        private readonly Type _eventType;
        private readonly Type _eventHandlerType;

        public HandlerUnregistrar(IEventBus eventBus, Type eventType, Type eventHandlerType)
        {
            _eventBus = eventBus;
            _eventType = eventType;
            _eventHandlerType = eventHandlerType;
        }

        private bool _isDisposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!_isDisposed)
            {
                if (disposing)
                {
                    _eventBus.Unregister(_eventType, _eventHandlerType);
                }

                _isDisposed = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
